const db = require('../../MySql')
const { formatDateTime } = require('../utils/dateUtils')
/**
 * 用户错题本实体类
 * 用于管理用户在考试中答错的题目记录
 */
class el_user_book {
    /**
     * 用户错题本构造函数
     * @param {string} id - 错题记录唯一标识符
     * @param {string} exam_id - 关联的考试ID
     * @param {string} user_id - 用户ID
     * @param {string} qu_id - 题目ID
     * @param {Date|string} create_time - 错题记录创建时间
     * @param {Date|string} update_time - 错题记录最后更新时间
     * @param {number} wrong_count - 错误次数统计
     * @param {string} title - 错题标题或题目内容
     * @param {number} sort - 排序序号
     */
    constructor(id, exam_id, user_id, qu_id, create_time, update_time, wrong_count, title, sort) {
        this.id = id
        this.exam_id = exam_id
        this.user_id = user_id
        this.qu_id = qu_id
        this.create_time = create_time
        this.update_time = update_time
        this.wrong_count = wrong_count
        this.title = title
        this.sort = sort
    }

    //根据id查询用户错题本
    static async findUserBookById(id) {
        const sql = `SELECT * FROM el_user_book WHERE id=?`
        const [results] = await db.query(sql, [id])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }

    //根据用户id和考试id查询用户错题本
    static async findUserBookByUserIdAndExamId(user_id, exam_id, current = 1, size = 10) {
        const sql = `SELECT * FROM el_user_book WHERE user_id=? AND exam_id=? ORDER BY sort LIMIT ?,?`
        const [results] = await db.query(sql, [user_id, exam_id, (current - 1) * size, size])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }

    //根据用户id和题目id查询用户错题本
    static async findUserBookByExamIdAndQuId(exam_id, qu_id) {
        const sql = `SELECT * FROM el_user_book WHERE exam_id=? AND qu_id=?`
        const [results] = await db.query(sql, [exam_id, qu_id])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }

    static async findUserBookByExamIdAndUserIdAndQuId(exam_id, user_id, qu_id) {
        const sql = `SELECT * FROM el_user_book WHERE exam_id=? AND user_id=? AND qu_id=?`
        const [results] = await db.query(sql, [exam_id, user_id, qu_id])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }


    //根据examId查询用户错题本
    static async findUserBookByExamId(exam_id) {
        const sql = `SELECT * FROM el_user_book WHERE exam_id=?`
        const [results] = await db.query(sql, [exam_id])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }

    //
    static async findUserBookByUserIdAndExamIdAndQuId(userId, examId, quId) {
        const sql = `SELECT * FROM el_user_book WHERE user_id=? and exam_id=? and qu_id =?`
        const [results] = await db.query(sql, [userId, examId, quId])
        return results.map(item => ({
            ...item,
            create_time: formatDateTime(item.create_time),
            update_time: formatDateTime(item.update_time),
        }))
    }

    //新增用户错题本
    static async addUserBook(user_book) {
        const sql = `INSERT INTO el_user_book (id,exam_id,user_id,qu_id,create_time,update_time,wrong_count,title,sort) VALUES (?,?,?,?,?,?,?,?,?)`
        const results = await db.query(sql, [user_book.id, user_book.exam_id, user_book.user_id, user_book.qu_id, user_book.create_time, user_book.update_time, user_book.wrong_count, user_book.title, user_book.sort])
        return results
    }

    //更新wront_count
    static async updateWrongCount(id, wrong_count) {
        const sql = `UPDATE el_user_book SET wrong_count=? WHERE id=?`
        const results = await db.query(sql, [wrong_count, id])
        return results
    }


    //删除用户错题本
    static async deleteUserBook(id) {
        const sql = `DELETE FROM el_user_book WHERE id=?`
        const results = await db.query(sql, [id])
        return results
    }

}

module.exports = el_user_book